window.addEventListener('load',function() {
	$('#title').append("<h1>" + "Quiz Summary" + "</h1>");
	$('#summary').hide();
	
	var jawaban_benar 	= localStorage.getItem("estudia_ans_right");
	var jawaban_salah 	= localStorage.getItem("estudia_ans_false");
	var tambahan_point	= (parseInt(jawaban_benar) * 20) + (parseInt(jawaban_salah) * 5);

	if (jawaban_benar == 5) {
		tambahan_point += 10;
	}
	
	var current_level	= localStorage.getItem("estudia_level");
	var current_exp		= localStorage.getItem("estudia_exp");
	
	var next_level		= parseInt(current_level);
	var next_exp 		= parseInt(current_exp) + tambahan_point;
	
	var username		= localStorage.getItem("estudia_user");
	
	if ( next_exp >= 100) {
		next_level 	+= 1;
		next_exp	-= 100;
		
		if ( next_exp >= 100) {
			next_level 	+= 1;
			next_exp	-= 100;
		}
		
		localStorage.setItem("estudia_level", next_level);
		localStorage.setItem("estudia_exp", next_exp);

	} else {
		localStorage.setItem("estudia_exp", next_exp);

	}
	
	$.get("http://estudiahapid.ap01.aws.af.cm/services/addpoint.php?username=" + username + "&lvl=" + next_level + "&exp=" + next_exp + "", function(data) {
		var result = JSON.parse(data);
		
		if (result["status"] == "success") {
			//alert("success");
		} else {
			//alert("failed");
		}
	}).done(function (data) {

	}); //*/
	
	$('#summary').show();
	$('#right_answer').append("<h3>= " + jawaban_benar + "</h3>");
	$('#wrong_answer').append("<h3>= " + jawaban_salah + "</h3>");
	$('#score').append("<h3>= " + (parseInt(jawaban_benar) * 2) + "/10</h3>");
	$('#point_addition').append("<h3>= " + tambahan_point + "</h3>");
		
	$( "#new_progressbar" ).progressbar({
	value: next_exp
	});
	$('#new_progressbar_detail').append("<h6>" + next_exp + " / " + "100" + "</h6>");

	if (current_level < next_level) {
		$('#new_level').append("<h2> Level " + next_level + " Up! </h2>");
	} else {
		$('#new_level').append("<h2> Level " + next_level + "</h2>");
	}
	
});

function capitalise(string) {
	return string.charAt(0).toUpperCase() + string.slice(1);
};